import 'package:flutter/material.dart';
import 'package:flutterapp/page/utils/scopoed_model_helper.dart';
import 'package:flutterapp/scoped_model/auth.dart';

var nickname;
var avatar;

class MyHeaderPage extends StatelessWidget {
  const MyHeaderPage({Key key}) : super(key: key);

  //没有登录
  Widget _notLoginBuilder(BuildContext context) {
    return Container(
      height: 85.0,
      color: Colors.green,
      child: GestureDetector(
        onTap: () {},
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Padding(
              padding: EdgeInsets.only(left: 20.0),
            ),
            //图片组件
            // CircleAvatar(
            //   backgroundImage: NetworkImage(
            //       'http://r0utoyzux.hn-bkt.clouddn.com/gwc.png'),
            // ),
            Container(
              width: 65.0,
              height: 65.0,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(70)),
                image: DecorationImage(
                  image: NetworkImage(
                      //http://r0utoyzux.hn-bkt.clouddn.com/fz.jpg
                      'http://r0utoyzux.hn-bkt.clouddn.com/gwc.png'),
                  fit: BoxFit.cover,
                ),
              ),
            ),
            Padding(
              padding: EdgeInsets.only(left: 20.0),
            ),
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                GestureDetector(
                  onTap: () {},
                  child: Row(
                    children: [
                      GestureDetector(
                        onTap: () {
                          Navigator.of(context).pushNamed('login');
                        },
                        child: Text('登录',
                            style:
                                TextStyle(color: Colors.white, fontSize: 18.0)),
                      ),
                      GestureDetector(
                        onTap: () {
                          Navigator.of(context).pushNamed('register');
                        },
                        child: Text('/注册',
                            style:
                                TextStyle(color: Colors.white, fontSize: 18.0)),
                      ),
                    ],
                  ),
                ),
                GestureDetector(
                  onTap: () {
                    Navigator.of(context).pushNamed('setting');
                  },
                  child: Container(
                    child: Text('编辑个人资料'),
                  ),
                )
              ],
            )
          ],
        ),
      ),
    );
  }

  //登录
  Widget _loginBuilder(BuildContext context) {
    try {
      var userInfo = ScopedModelHelper.getModel<AuthModel>(context).userInfo;

      nickname = userInfo.nickname.toString();
      avatar = userInfo.avatar.toString();
    } catch (e) {}

    return Container(
      height: 85.0,
      color: Colors.green,
      child: GestureDetector(
        onTap: () {},
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Padding(
              padding: EdgeInsets.only(left: 20.0),
            ),
            //图片组件
            // CircleAvatar(
            //   backgroundImage: NetworkImage(
            //       'http://r0utoyzux.hn-bkt.clouddn.com/gwc.png'),
            // ),
            Container(
              width: 65.0,
              height: 65.0,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(70)),
                image: DecorationImage(
                  image: NetworkImage(
                      //http://r0utoyzux.hn-bkt.clouddn.com/fz.jpg
                      avatar),
                  fit: BoxFit.cover,
                ),
              ),
            ),
            Padding(
              padding: EdgeInsets.only(left: 20.0),
            ),
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                GestureDetector(
                  onTap: () {},
                  child: Row(
                    children: [
                      GestureDetector(
                        onTap: () {
                          //Navigator.of(context).pushNamed('login');
                        },
                        child: Text(nickname,
                            style:
                                TextStyle(color: Colors.white, fontSize: 18.0)),
                      ),
                    ],
                  ),
                ),
                GestureDetector(
                  onTap: () {
                    Navigator.of(context).pushNamed('setting');
                  },
                  child: Container(
                    width: 230.0,
                    child: Text(
                      //'风是流动的让我们看到大自然的风云变换,水是流动的,让我们体会到物质的生生不息,人是流动的,让我们看到生存的转机!',
                      '查看个人资料',
                      overflow: TextOverflow.ellipsis,
                    ),
                  ),
                )
              ],
            )
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    var isLogin = ScopedModelHelper.getModel<AuthModel>(context).isLogin;
    // var isLogin = true;
    //ScopedModelHelper.getModel<AuthModel>(context).
    return isLogin ? _loginBuilder(context) : _notLoginBuilder(context);
  }
}
